package ruleta_wicket.ruleta_wicket;

import java.io.Serializable;

import org.apache.wicket.ajax.AbstractAjaxTimerBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.time.Duration;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.ui.dialog.Dialog;
import org.odlabs.wiquery.ui.tabs.Tabs;
import domain.Player;
import domain.extra.Result;

public class TablePage extends WebPage implements Serializable{

	private static final long serialVersionUID = 1L;
	private Tabs tabs;
	private int count;
	private Dialog dialog;
	private String result;
	final int numberCount = 10000; 
	
	public TablePage() {
		super();
	}

	public TablePage(Player player) {
		super();
		this.tabs = new Tabs("tabs");
		this.add(tabs);
		this.addTabs(player);
		
		Form<Player> form = new Form<Player>("datosPlayer", new CompoundPropertyModel<Player>(player));
		this.add(form);
		this.addHeader(form, player);
		this.addInfoPlayer(form, player);
		
		final Label label = new Label("count", new PropertyModel<LogginPage>(this, "count"));
		label.setOutputMarkupId(true);
		this.add(label);
		this.restartCount(); 
		this.timer(label);

		dialog = new Dialog("alert");
		MultiLineLabel resultLabel = new MultiLineLabel("result", new PropertyModel<String>(this, "result"));		
		resultLabel.setOutputMarkupId(true);
		dialog.add(resultLabel);
		this.add(dialog);
	}

	private void timer(final Label label) {
		this.add(new AbstractAjaxTimerBehavior(Duration.milliseconds(100)) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onTimer(AjaxRequestTarget target) {
				if (getCount() != 0) {
					decreaseCount(); 
					target.addComponent(label);
				} else {
					result = (WicketApplication.getApplication().getTable().rouletteSpin()).toString();
					WicketApplication.getApplication().getTable().betsPay();
					dialog.setTitle("Se ha girado la ruleta!");
					restartCount(); 
					target.addComponent(dialog);
					boolean isNotOpen = true;
					CharSequence fooVar = null;						
					if (isNotOpen){
						fooVar = dialog.open().render();
						isNotOpen = false;
					}
					target.appendJavascript(fooVar.toString());					
				}
			}
		});
	}

	private void addHeader(Form<Player> form, final Player player) {
		form.add(new Link<Player>("logout") {
			@Override
			public void onClick() {
				WicketApplication.getApplication().getTable().removePlayer(player);
				this.setResponsePage(HomePage.class);
			}
		});
	}

	private void addTabs(final Player player) {
		tabs.add(new ChangeChipsPanel("changeChips", player));
		tabs.add(new ToBetPanel("apostar", player, this));
		tabs.add(new ListPlayerPanel());
//		tabs.add(new TableroPanel("tableroApuestas"));
		}

	private void addInfoPlayer(Form<Player> form, Player player) {
		Label nombre = new Label("name");
		Label dineroLabel = new Label("money");
		Label chipsLabel = new Label("chips");

		form.add(nombre);
		form.add(dineroLabel);
		form.add(chipsLabel);
	}

	public Tabs getTabs() {
		return tabs;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	public void restartCount (){
		this.count = numberCount; 
	}
	
	public void decreaseCount(){
		this.count --; 
	}

	public int getCount() {
		return count;
	}
}
